/****************************************柱图上显示具体数值*****************************************/
//自定义一个继承于QCPBars类，并重写其虚数函数draw（）

#include "custombar.h"

CustomBar::CustomBar(QCPAxis *keyAxis, QCPAxis *valueAxis)
    : QCPBars(keyAxis, valueAxis),
    mTextAlignment(Qt::AlignCenter),
    mSpacing(5),
    mFont(QFont(QLatin1String("sans serif"), 11)),
    mMinValue(valueAxis->range().lower)
{

}

void CustomBar::setTextAlignment(Qt::Alignment alignment)
{
    mTextAlignment = alignment;
}

void CustomBar::setSpacing(double spacing)
{
    mSpacing = spacing;
}

void CustomBar::setFont(const QFont &font)
{
    mFont = font;
}

void CustomBar::draw(QCPPainter *painter)
{
    if (!mKeyAxis || !mValueAxis) { qDebug() << Q_FUNC_INFO << "invalid key or value axis"; return; }
        if (mDataContainer->isEmpty()) return;

        QCPBarsDataContainer::const_iterator visibleBegin, visibleEnd;
        getVisibleDataBounds(visibleBegin, visibleEnd);

        // loop over and draw segments of unselected/selected data:
        QList<QCPDataRange> selectedSegments, unselectedSegments, allSegments;
        getDataSegments(selectedSegments, unselectedSegments);
        allSegments << unselectedSegments << selectedSegments;
        for (int i = 0; i < allSegments.size(); ++i)
        {
            bool isSelectedSegment = i >= unselectedSegments.size();
            QCPBarsDataContainer::const_iterator begin = visibleBegin;
            QCPBarsDataContainer::const_iterator end = visibleEnd;
            mDataContainer->limitIteratorsToDataRange(begin, end, allSegments.at(i));
            if (begin == end)
                continue;

            for (QCPBarsDataContainer::const_iterator it = begin; it != end; ++it)
            {
                // check data validity if flag set:
    #ifdef QCUSTOMPLOT_CHECK_DATA
                if (QCP::isInvalidData(it->key, it->value))
                    qDebug() << Q_FUNC_INFO << "Data point at" << it->key << "of drawn range invalid." << "Plottable name:" << name();
    #endif
                // draw bar:
                if (isSelectedSegment && mSelectionDecorator)
                {
                    mSelectionDecorator->applyBrush(painter);
                    mSelectionDecorator->applyPen(painter);
                }
                else
                {
                    painter->setBrush(mBrush);
                    painter->setPen(mPen);
                }
                applyDefaultAntialiasingHint(painter);
    //以上均为源码
                QRectF barRect = getBarRect(it->key, it->value);
                painter->drawPolygon(getBarRect(it->key, it->value));
                painter->setPen(QPen(Qt::white));
                painter->setFont(mFont);
                //qDebug()<<"sdf d ds" << it->value;
                QString text = QString::number(it->value, 'f', 1);
                QRectF textRect = painter->fontMetrics().boundingRect(0, 0, 0, 0, Qt::TextDontClip | mTextAlignment, text);  // 计算文字所占用的大小

                if (mKeyAxis.data()->orientation() == Qt::Horizontal) {    // 当key轴为水平轴的时候
                    if (mKeyAxis.data()->axisType() == QCPAxis::atTop)     // 上轴，移动文字到柱状图下面
                        textRect.moveTopLeft(barRect.bottomLeft() + QPointF(0, mSpacing));
                    else                                                   // 下轴，移动文字到柱状图上面
                    {
                        if (it->value >= mMinValue)//如果标签可见
                        {

                            textRect.moveBottomLeft(barRect.topLeft() - QPointF(0, mSpacing));

                        }
                        else//如果标签可不见
                        {

                            double PixN = ((barRect.bottomLeft().y() - barRect.topLeft().y())/ it->value)*mMinValue;
                            qDebug() << PixN <<"  "<< barRect.bottomLeft().y()<<"  " << barRect.topLeft().y();
                            textRect.moveBottomLeft(barRect.bottomLeft()- QPointF(0, mSpacing+PixN));
                            qDebug() << "n:" << it->value << "   " << mMinValue << barRect.bottomLeft() - QPointF(0, mSpacing + PixN);
                        }



                    }
                    textRect.setWidth(barRect.width());
                    painter->drawText(textRect, Qt::TextDontClip | mTextAlignment, text);
                }
                else {
                    if (mKeyAxis.data()->axisType() == QCPAxis::atLeft)
                        textRect.moveTopLeft(barRect.topRight() + QPointF(mSpacing, 0));
                    else
                        textRect.moveTopRight(barRect.topLeft() - QPointF(mSpacing, 0));
                    textRect.setHeight(barRect.height());
                    painter->drawText(textRect, Qt::TextDontClip | mTextAlignment, text);
                }

            }
        }

        // draw other selection decoration that isn't just line/scatter pens and brushes:
        if (mSelectionDecorator)
            mSelectionDecorator->drawDecoration(painter, selection());

}

